es cheat sheet

es cheat sheet
http://elasticsearch-cheatsheet.jolicode.com

exploring cluster

cluster health

1
2
3
4
# 健康度
curl -X GET "localhost:9200/_cat/health?v"
# 获取节点
curl -X GET "localhost:9200/_cat/nodes?v"

list all indices

1
curl -X GET "localhost:9200/_cat/indices?v"

create index

1
2
3
4
# 先创建一个index
curl -X PUT "localhost:9200/customer?pretty"
# 再查看一下
curl -X GET "localhost:9200/_cat/indices?v"

文档增删改查

插入并获取指定文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 插入
# 指定ID(如下用的1)的时候,用的是PUT方式
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'

# 不指定ID,要用POST
curl -X POST "localhost:9200/customer/_doc?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Doe"
}
'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 获取
curl -X GET "localhost:9200/customer/_doc/1?pretty"
# 响应
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "John Doe"
}
}

更新文档

1
2
3
4
5
6
7
8
9
10
11
12
# update路径,修改name,并增加age
curl -X POST "localhost:9200/customer/_update/1?pretty" -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe", "age": 20 }
}
'
# 可以用简单的script直接修改增量等操作
curl -X POST "localhost:9200/customer/_update/1?pretty" -H 'Content-Type: application/json' -d'
{
"script" : "ctx._source.age += 5"
}
'

删除文档

1
curl -X DELETE "localhost:9200/customer/_doc/2?pretty"

批量处理

1
2
3
4
5
6
7
8
9
10
# 插入俩文档,更新一个,删除一个
curl -X POST "localhost:9200/customer/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
{"update":{"_id":"3"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"4"}}
'

文档高级检索

1
2
3
4
5
6
7
8
9
curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
# 指定返回的文档字段
"_source": ["account_number", "balance"]
"query": { "match_all": {} },
"from": 10,
"size": 10
}
'

匹配address包含mill的结果

1
2
3
4
GET /bank/_search
{
"query": { "match": { "address": "mill" } }
}

匹配address包含mill或者lane的结果

1
2
3
4
GET /bank/_search
{
"query": { "match": { "address": "mill lane" } }
}

匹配address包含”mill lane”短语的结果

1
2
3
4
GET /bank/_search
{
"query": { "match_phrase": { "address": "mill lane" } }
}

and 语句

1
2
3
4
5
6
7
8
9
10
11
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}

OR 语句

1
2
3
4
5
6
7
8
9
10
11
GET /bank/_search
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}

must not

1
2
3
4
5
6
7
8
9
10
11
GET /bank/_search
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}

must & must not

1
2
3
4
5
6
7
8
9
10
11
12
13
GET /bank/_search
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}

检索优化

  • query和filter的区别
    query会计算相关度
    filter不会计算相关度,而且检索结果会有缓存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
GET /bank/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}

聚合

https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started-aggregations.html

如果只需要聚合结果,size 填 0 即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
GET /bank/_search
{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_by_gender": {
"terms": {
"field": "gender.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}